package com.lzj.hdfs2hbase;

import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, ImmutableBytesWritable, Mutation>.Context context) throws IOException, InterruptedException {
        //创建一个累加器
        int count = 0;
        //开始迭代value
        for (IntWritable value : values) {
            count += value.get();
        }
        //创建插入数据对象put
        Put put = new Put("harry".getBytes());
        put.addColumn("info".getBytes(), key.getBytes(), String.valueOf(count).getBytes());
        //写出
        context.write(null, put);
    }

}